<?php
include '../../../wp-blog-header.php';

$deal_id = $_POST['deal_id'];
$consumer_id = $_POST['consumer_id'];

$users = new Pod('users');
$users->page = 1;
$users->search = 1;
$users->findRecords('id DESC', -1, "t.id ='$consumer_id'");
while ($users->fetchRecord())
{ 
  $deals_liked = $users->get_field('deals_liked.id');
}

$points = new Pod('points');
$points->page = 1;
$points->search = 1;
$points->findRecords('id DESC', -1, "user.id ='$consumer_id'");
while ($points->fetchRecord())
{ 
  $points_id = $points->get_field('id');
  $vote_points = $points->get_field('vote');
}

if(!empty($deals_liked))
{
  if(is_array($deals_liked))
  {
    if(!in_array($deal_id, $deals_liked))
    {
       $deals_liked_ids = implode(',',$deals_liked);
       $deals_liked_ids .= ','.$deal_id;
    }
  }
  else
  {
    if($deals_liked != $deal_id)
    {
       $deals_liked_ids = $deals_liked.','.$deal_id;
    }
  }
}
else
{
  $deals_liked_ids = $deal_id;
}

if(!empty($deals_liked_ids))
{
  $api = new PodAPI();
  
  $columns_user['deals_liked'] = $deals_liked_ids; 
  
  $params_user = array('datatype' => 'users', 'tbl_row_id' => $consumer_id, 'columns' => $columns_user);
  $api->save_pod_item($params_user);
  
  $columns_points['vote'] = $vote_points + 10;
  $params_points = array('datatype' => 'points', 'tbl_row_id' => $points_id, 'columns' => $columns_points);
  $api->save_pod_item($params_points);
  
  $deal = new Pod('deal');
  $deal->page = 1;
  $deal->search = 1;
  $deal->findRecords('id DESC', -1, "t.id ='$deal_id'");
  while ($deal->fetchRecord())
  { 
    $user_likes = $deal->get_field('user_likes.id');
  }
  
  if(empty($user_likes))
  {
    $count_likes = '0 Likes';
  }
  else
  {
    if(is_array($user_likes))
    {
      $count_likes = count($user_likes).' Likes'; 
    }
    else
    {
      $count_likes = '1 Like';
    }
  }
  
  
  echo '<img src="'.BUZZR_WP_URL.'/wp-content/pfiles/images/success.gif" />&nbsp;&nbsp;<font color="red">'.$count_likes.'</font>';
}
?>